# -*- coding:utf-8 -*- #

from pyinotify import WatchManager, Notifier, ProcessEvent, \
     IN_CLOSE_WRITE,IN_CREATE,IN_MOVED_FROM,IN_DELETE,IN_Q_OVERFLOW,IN_MOVED_TO

#import sys
#import os
#sys.path.append(os.pardir)
#from priority import rootHomeDir

##监控文件是否有变化

class EventHandler(ProcessEvent):
    
    """对监控事件的处理"""
    def add_clock(self, clock):
        """ clock is a threading.Event"""
        self.clock = clock
        
    def process(self, event):
        """after any file watched is change, make clock set"""
        #event.pathname 得到修改的路径+文件名
        #监控conf文件夹下configure.py文件
        if event.pathname.endswith('configure.py'):
            self.clock.set()
            #打印事件，例子
            # <Event dir=False mask=0x8 maskname=IN_CLOSE_WRITE name=configure.py path=/root/monitor/probe/conf pathname=/root/monitor/probe/conf/configure.py wd=1 >
            print event

    #def process_IN_CREATE(self, event):
        #"""docstring for process_IN_CREATE"""
        #self.process(event)

    def process_IN_CLOSE(self, event):
        self.process(event)

    #def process_IN_MODIFY(self, event):
        #self.process(event)

    #def process_IN_DELETE(self, event):
        #self.process(event)

    #def process_IN_ACCESS(self, event):
        #self.process(event)

    #def process_IN_MOVED_FROM(self, event):
        #self.process(event)

    #def process_IN_MOVED_TO(self, event):
        #self.process(event)

    def Process_IN_CLOSE_WRIT(self, event):
        self.process(event)


def confMonitor(event,args_path):
    import os

    clock = event
    
    #pathname = os.path.join(rootHomeDir(),'conf')
    pathname = args_path

    mask = (IN_CLOSE_WRITE | IN_CREATE |
            IN_MOVED_FROM | IN_DELETE |
            IN_Q_OVERFLOW | IN_MOVED_TO)

    wm = WatchManager()
    p = EventHandler()
    p.add_clock(clock)
    
    notifier = Notifier(wm, p)
    
    wm.add_watch(pathname,
            mask, rec=False, auto_add=True)

    while True:
        try:
            notifier.process_events()
            if notifier.check_events():
                notifier.read_events()
        except KeyboardInterrupt:
            notifier.stop()
            break

#if __name__ == '__main__':
    #confMonitor()